package com.jeesite.modules.kj.utils;

import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.network.IpUtils;
import com.jeesite.common.utils.SpringUtils;
import com.jeesite.common.web.http.UserAgentUtils;
import com.jeesite.modules.kj.service.KjSkillService;
import com.jeesite.modules.sys.entity.Log;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.support.LogServiceSupport;
import eu.bitwalker.useragentutils.UserAgent;

import javax.servlet.http.HttpServletRequest;

public class SystemLogUtil {

    /**
     * 静态内部类，延迟加载，懒汉式，线程安全的单例模式
     */
    private static final class Static {
        private static LogServiceSupport logServiceSupport = SpringUtils.getBean(LogServiceSupport.class);
    }

    //插入管理员操作
    public static void insertAdminOperate(User user, String logTitle, String logType, String bizType, String bizKey, HttpServletRequest request){
        Log log = new Log();
        log.setBizKey(bizKey);
        log.setBizType(bizType);
        log.setLogTitle(logTitle);
        log.setLogType(logType);
        log.setServerAddr(request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort());
        log.setRemoteAddr(IpUtils.getRemoteAddr(request));
        UserAgent userAgent = UserAgentUtils.getUserAgent(request);
        log.setDeviceName(userAgent.getOperatingSystem().getName());
        log.setBrowserName(userAgent.getBrowser().getName());
        log.setUserAgent(request.getHeader("User-Agent"));
        log.setRequestUri(StringUtils.abbr(request.getRequestURI(), 255));
        log.setRequestParams(request.getParameterMap());
        log.setRequestMethod(request.getMethod());
        log.setExecuteTime(200L);
        log.setCurrentUser(user);
        Static.logServiceSupport.insertLog(log);
//        log.set

    }
}
